/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBLayer;

import DomainLayer.Customer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Rose
 */
public class CustomerDB implements CustomerIFDB{
    private Connection con;
    
    public CustomerDB(){
        con = ConnectorDB.getInstance().getConDB();
    }

    public ArrayList getAllCustomers(boolean retriveAssociation) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Customer findCustomer(int customerID, boolean retriveAssociation) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
      public int insertCustomer(Customer customer) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int deleteCustomer(int customerID) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    private String buildQuery(String wClause)
    {
        String query = " SELECT CustomerID, Name,Address, ZipCode, City, Phone FROM Customer";

        if( wClause.length()>0)
            query=query + " WHERE "+ wClause;

        return query;
    }
    private Customer buildCustomer(ResultSet results)
    {
        Customer custObj = new Customer();
        try{
            custObj.setCustomerID(results.getString("CustomerID"));
            custObj.setName(results.getString("Name"));
            custObj.setZipCode(results.getInt("Zipcode"));
            custObj.setCity(results.getString("City"));
            custObj.setPhoneNo(results.getString("PhoneNo"));
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        return custObj;
    }


    //private methods
        //michWere is used whenever we want to select more than one customer
    private ArrayList<Customer> miscWhere(String wClause, boolean retrieveAssociation)
    {
        ResultSet results;
        ArrayList<Customer> list = new ArrayList <Customer>();

        String query = buildQuery(wClause);

         try{ // read the customer from the database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            int snr =0;   // WHAT IS THIS???
            while(results.next()){
                Customer custObj = new Customer();
                custObj = buildCustomer(results);
                list.add(custObj);
            }
            stmt.close();
            return list;
         }

         catch(Exception e)
         {
            System.out.println (e.getMessage());
            return null;
         }
    }

      //Singelwhere is used when we only select one employee
    private Customer singleWhere( String wClause, boolean retrieveAssociation)
    {
         ResultSet results;
         Customer custObj = new Customer();

         String query = buildQuery(wClause);
         System.out.println(query);
         try{// read the customer from the database
             Statement stmt = con.createStatement();
             stmt.setQueryTimeout(5);
             results =stmt.executeQuery(query);
             if( results.next()){
                 custObj = buildCustomer(results);
             }
                 // association is to be build
                 stmt.close();
                 return custObj;
             
         }
         catch(Exception e)
         {
             System.out.println(e.getMessage());
             return null;
         }
    }



}
